<template>
  <el-row :gutter="25">
    <el-col :span="7">
      <el-divider><span class="divider-css">选择接诊信息</span>
        <el-tooltip class="box-item" content="请先选择需要查看的患者就诊信息" effect="dark" placement="top">
          <i-ep-infoFilled class="infoFilled-style"/>
        </el-tooltip>
      </el-divider>
      <el-button class="mb5" plain style="float: right;" type="primary"
                 @click="listClinicMasterFun(props.patientId)">
        <template #icon>
          <i-ep-search/>
        </template>
        刷新
      </el-button>
      <el-table :border="true" :data="patientList" :header-cell-style="$headerCellStyle" :stripe="true" height="260px"
                highlight-current-row @row-click="patientRowClick">
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="门诊号" prop="clinicNo"
                         width="110"/>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="患者姓名" prop="name"/>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="接诊科室" prop="deptName"
                         width="80"/>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="接诊医生" prop="doctorName"
                         width="80"/>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="接诊时间" prop="visitDate"
                         width="100"/>
      </el-table>
    </el-col>
    <el-col :span="17">
      <el-divider><span class="divider-css">选择处方信息</span>
        <el-tooltip class="box-item" content="选择好患者就诊信息后，可以查看此次就诊开具的处方单，点击某一行处方，
                可以查看明细，点击【处方引用】可直接引用处方" effect="dark" placement="top">
          <i-ep-infoFilled class="infoFilled-style"/>
        </el-tooltip>
      </el-divider>
      <el-button class="mb5 mr10" plain style="float: right;" type="primary" @click="listPrescriptionApplyFun()">
        <template #icon>
          <i-ep-search/>
        </template>
        刷新
      </el-button>
      <el-table ref="applyTableRef" :border="true" :data="prescriptionApplyList" :header-cell-style="$headerCellStyle"
                :stripe="true"
                height="260px" highlight-current-row @row-click="applyRowClick">
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="申请序号" prop="applyNo"
                         width="110"/>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="病种" prop="diseaseInformationName"
                         width="70">
          <template #default="scope">
            {{ scope.row.diseaseInformationName }}
          </template>
        </el-table-column>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="处方属性" prop="prescAttr"
                         width="110">
          <template #default="scope">
            <dict-data-tag :options="dictDataMap['prescription_type']" :value="scope.row.prescAttr"/>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="开单科室" prop="applyDeptName" width="130"/>
        <el-table-column :align="'center'" label="开单时间" prop="createTime" width="150"/>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="收费标志" prop="chargeFlag"
                         width="80">
          <template #default="scope">
            <dict-data-tag :options="dictDataMap['charge_flag_dict']" :value="scope.row.chargeFlag"/>
          </template>
        </el-table-column>
        <!--        <el-table-column :align="'center'" label="总费用" prop="sumPrice" width="130"/>-->
        <el-table-column :align="'center'" label="发药药房" prop="implementDeptName"/>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="发药标志" prop="confirmFlag"
                         width="90">
          <template #default="scope">
            <dict-data-tag :options="dictDataMap['grant_flag']" :value="scope.row.confirmFlag"/>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="诊断" prop="icdName" width="130"/>
        <el-table-column :align="'center'" class-name="small-padding fixed-width" fixed="right"
                         label="操作"
                         width="110">
          <template #default="scope">
            <el-button class="mb5" type="success"
                       @click.stop="applyQuote(scope.row)">
              <template #icon>
                <i-ep-edit/>
              </template>
              处方引用
            </el-button>
          </template>
        </el-table-column>

      </el-table>
    </el-col>
  </el-row>
  <el-row :gutter="15">
    <el-col :span="24">
      <!-- ，勾选（或者多选）后，点击【明细引用】可直接引用处方明细 -->
      <el-divider><span class="divider-css">处方明细信息</span>
        <el-tooltip class="box-item" content="查看本次处方开具的药品明细" effect="dark" placement="top">
          <i-ep-infoFilled class="infoFilled-style"/>
        </el-tooltip>
      </el-divider>
      <!-- <el-button style="float: right;" class="mb5" type="success" :disabled="itemDisabled" @click="itemQuote()">
          <template #icon>
              <i-ep-edit />
          </template>明细引用
      </el-button> -->
      <!-- <el-button style="float: right;" class="mb5" plain type="primary" @click="listPrescriptionItemFun()">
          <template #icon>
              <i-ep-search />
          </template>
          刷新
      </el-button> -->
      <!-- @selection-change="itemSelectionChange"  -->
      <el-table v-adaptive="600" :border="true" :data="prescriptionItemList" :header-cell-style="$headerCellStyle"
                :stripe="true"
                highlight-current-row>
        <!-- <el-table-column :align="'center'" fixed="left" type="selection" width="45" /> -->
        <el-table-column :align="'center'" label="组号" prop="groupNumber" width="60"/>
        <el-table-column :align="'center'" label="药品" prop="drugName"/>
        <el-table-column :align="'center'" label="途径" prop="channel" width="110">
          <template #default="scope">
            <span>{{ scope.row.channelName }}</span>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="单次用量" prop="singleDosage" width="120">
          <template #default="scope">
                        <span style="margin-left: -15px"> {{ scope.row.singleDosage }}<dict-data-tag
                          :options="dictDataMap['dose_unit']" :value="scope.row.singleUnit"
                          style="margin: -23px -20px 0px 35px"/></span>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="频次" prop="performFreq" width="110">
          <template #default="scope">
            <span>{{ scope.row.performFreqName }}</span>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="天数" prop="abidance" width="80">
          <template #default="scope">
            <span>{{ scope.row.abidance }}</span>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="数量" prop="num" width="100">
          <template #default="scope">
                        <span>{{ scope.row.num }} <dict-data-tag :options="dictDataMap['spec_unit']"
                                                                 :value="scope.row.unit"
                                                                 style="margin: -23px -20px 0px 27px"/></span>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="免皮试" prop="noSkin" width="80px">
          <template #default="scope">
            <dict-data-tag :options="dictDataMap['sys_yes_no']" :value="scope.row.noSkin"/>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="单价" prop="price" width="100">
          <template #default="scope">
            {{ parseFloat(scope.row.price).toFixed(2) }}元
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="总价" prop="sumPrice" width="100">
          <template #default="scope">
            {{ parseFloat(scope.row.sumPrice).toFixed(2) }}元
          </template>
        </el-table-column>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="规格" prop="packageSpec"
                         width="150"/>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="厂商" prop="firmName"
                         width="190">
          <template #default="scope">
            <div>{{ scope.row.firmName }}</div>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" :show-overflow-tooltip="true" label="医生说明" width="100">
          <template #default="scope">
            <span>{{ scope.row.remarks }}</span>
          </template>
        </el-table-column>
        <el-table-column :align="'center'" label="滴速" prop="dripSpeed" width="110px">
          <template #default="scope">
            <dict-data-tag :options="dictDataMap['sys_drop_speed']" :value="scope.row.dripSpeed"/>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>
</template>

<script lang="ts" setup>
import {useDictStoreHook} from "@/store/modules/dict";
import {listClinicMaster} from "@/api/patient/clinicMaster.ts";
import {listPrescriptionApply} from "@/api/patient/prescriptionApply.ts"
import {listPrescriptionItem} from "@/api/patient/prescriptionItem.ts"

const dictStore = useDictStoreHook();
const emits = defineEmits(["applyQuote"])
const props = defineProps({
  patientId: {
    type: String,
    default: ""
  },
  refreshKey: {
    type: Number,
    default: 0
  },
  orgId: {
    type: String,
    default: ""
  },
  inoroutFlag: {
    type: String,
    default: ""
  },
})
let dictDataMap = ref(dictStore.isTypeGetData('prescription_type', 'bill_item_class_dict', 'dose_unit', 'spec_unit', 'sys_drop_speed',
  'sys_yes_no', 'charge_flag_dict', 'grant_flag', 'disease_information_dict'))
// 患者就诊记录id
const clinicId = ref<string>("")
// 已选择的患者处方主表信息
const applyId = ref<string>("")
// 患者list
const patientList = ref<any>([])
// 处方主记录list
const prescriptionApplyList = ref<any>([])
// 处方明细list
const prescriptionItemList = ref<any>([])
// 处方引用按钮是否禁用
const applyDisabled = ref<boolean>(true)
// 处方明细信息id 集合
const itemIds = ref<any>([])
// 处方明细信息按钮是否禁用
const itemDisabled = ref<boolean>(true)
const applyTableRef = ref()
watch(() => props.refreshKey, () => {
  listClinicMasterFun(props.patientId)
})

listClinicMasterFun(props.patientId)

/**
 * 获取patientId相同的患者列表
 * @param nowValue
 */
function listClinicMasterFun(nowValue: string) {
  patientList.value = []
  prescriptionApplyList.value = []
  prescriptionItemList.value = []
  const param = {
    patientId: nowValue
  }
  listClinicMaster(param).then((res: resType) => {
    patientList.value = res.data
  })
}

/**
 * 患者列表点击事件
 */
function patientRowClick(row: any) {
  prescriptionApplyList.value = []
  prescriptionItemList.value = []
  clinicId.value = row.id
  listPrescriptionApplyFun()
}

/**
 * 查询处方主记录
 */
function listPrescriptionApplyFun() {
  const param = {
    clinicId: clinicId.value,
    patientId: props.patientId,
    inoroutFlag: props.inoroutFlag,
    orgId: props.orgId
  }
  if (!clinicId.value) {
    return ElMessage.error("请先选择接诊信息后，再刷新。")
  }
  prescriptionItemList.value = []
  listPrescriptionApply(param).then((response: any) => {
    prescriptionApplyList.value = response.data.prescriptionApplyVos;
  });
}

// 处方主表点击事件
function applyRowClick(row: any) {
  applyId.value = row.applyId
  // 勾选
  applyTableRef.value.toggleRowSelection(row, true);
  listPrescriptionItemFun()
}

function listPrescriptionItemFun() {
  if (!applyId.value) {
    return ElMessage.error("请先选择处方信息后，再刷新。")
  }
  listPrescriptionItem({applyId: applyId.value, orgId: props.orgId}).then((res: any) => {
    prescriptionItemList.value = []
    res.data.forEach((item: any) => {
      item.isShow = true
      // 设置免皮试 如果是不能编辑状态 noSkin 设置为 true或者false，用于多选按钮选中，否则设置为 Y或者N，用于字典显示
      item.singleUnitName = dictStore.getMateDictInfo(dictDataMap.value['dose_unit'], item.singleUnit).dictLabel
      item.unit = item.packSize === "1" ? item.minUnits : item.packageUnits
      item.unitName = dictStore.getMateDictInfo(dictDataMap.value['spec_unit'], item.unit).dictLabel
      prescriptionItemList.value.push(item)
    })
  })
}

/**
 * 处方明细信息多选事件
 */
function itemSelectionChange(selection: any) {
  itemIds.value = selection.map(item => item.itemId)
  itemDisabled.value = selection.length <= 0
}

/**
 * 处方信息引用事件
 */
function applyQuote(row: any) {
  emits("applyQuote", row)
}

/**
 * 处方明细信息引用事件
 */
function itemQuote() {
}
</script>

<style scoped>
.infoFilled-style {
  margin-bottom: -3px;
}
</style>

<style scoped>
</style>
